//
//  HStoreManager+Drop.swift
//  Drop
//
//  Created by hushaohua on 2019/2/1.
//  Copyright © 2019 init. All rights reserved.
//

import Foundation
import CoreData

extension HStoreManager {
    
    func defaultTagNames() -> [String] {
        let strings:[String] = ["学习", "休息", "工作", "睡觉"];
        return strings;
    }
    
    func defaultEnTagNames() -> [String] {
        let strings:[String] = ["Read", "Rest", "Work", "Sleep"];
        return strings;
    }
    
    func loadStoreIf() {
        
        let request:NSFetchRequest<NSDictionary> = NSFetchRequest(entityName: "Drop");
        request.resultType = .dictionaryResultType;
        request.propertiesToFetch = ["begin"];
        request.fetchLimit = 1;

        do{
            let infos = try HStoreManager.defaultManger().context.fetch(request);
            if (infos.count > 0){
                let language:String? = NSLocale.preferredLanguages.first as String?;
                let isChinese = language!.hasPrefix("zh");
                if isChinese {
                    let names = self.defaultTagNames();
                    for name in names {
                        Tag.newInstance().updateName(name, isEn: isChinese);
                    }
                }else{
                    let names = self.defaultEnTagNames();
                    for name in names {
                        Tag.newInstance().updateName(name, isEn: isChinese);
                    }
                }
                self.saveContext();
            }
        }catch{
        }
    }
    
    
    
    
    
}
